package com.cn.transfer.jobhandler.purchaserequisition;

import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.cn.transfer.common.constant.KeyConstant;
import com.cn.transfer.common.entity.VvPuPurchaseRequisition;
import com.cn.transfer.common.entity.VvPuPurchaseRequisitionB;
import com.cn.transfer.common.entity.VvSaSaleOrder;
import com.cn.transfer.common.entity.VvSaSaleOrderB;
import com.cn.transfer.common.entity.jiandaoyun.JdyPurchaseOrder;
import com.cn.transfer.common.http.ApiClient;
import com.cn.transfer.common.utils.ConnmeUtils;
import com.cn.transfer.handle.impl.purchase.community.PurchaseCommunity;
import com.cn.transfer.mapper.VvPuPurchaseRequisitionBMapper;
import com.cn.transfer.mapper.VvPuPurchaseRequisitionMapper;
import com.xxl.job.core.handler.annotation.XxlJob;
import lombok.extern.slf4j.Slf4j;
import org.apache.http.Header;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author: 向伟典
 * @date: 2023/9/21 11:32
 * @Description: 请购单定时更新
 */
@Component
public class PurchaseRequisitionUpdateXxlJob {
    @Autowired
    private VvPuPurchaseRequisitionMapper vvPuPurchaseRequisitionMapper;
    @Autowired
    private VvPuPurchaseRequisitionBMapper vvPuPurchaseRequisitionBMapper;

    @Transactional
    @XxlJob("RequisitionUpdate")
    public void requisitionUpdate() {
        List<VvPuPurchaseRequisition> vvPuPurchaseRequisitions = vvPuPurchaseRequisitionMapper.byChangedateList();
        int i = 0;
        for (VvPuPurchaseRequisition vvPuPurchaseRequisition : vvPuPurchaseRequisitions) {
            if (vvPuPurchaseRequisitionMapper.inquireIsExist(vvPuPurchaseRequisition.getPurchaserequisitionid()) == 0) {
                continue;
            }
            VvPuPurchaseRequisition vvPuPurchaseRequisition1 = vvPuPurchaseRequisitionMapper.byDocumentnumber(vvPuPurchaseRequisition.getPurchaserequisitionid());// 表单
            boolean b = ConnmeUtils.deepEquals(vvPuPurchaseRequisition, vvPuPurchaseRequisition1);
            if (!b) {
                if (vvPuPurchaseRequisitionMapper.inquireIsExist(vvPuPurchaseRequisition.getPurchaserequisitionid()) > 0) {
                    // 不一致同步到视图和简道云
                    vvPuPurchaseRequisitionMapper.deletPurchaseRequisition(vvPuPurchaseRequisition.getPurchaserequisitionid());
                    vvPuPurchaseRequisitionBMapper.deletePurchaseRequisitionB(vvPuPurchaseRequisition.getPurchaserequisitionid());
                }
                Header[] headers = ConnmeUtils.createHeaders();
                Map<String, Object> parameterDelete = new HashMap<>();
                parameterDelete.put("app_id", "64f00ef131adb70007ad6e6d");
                parameterDelete.put("entry_id", "650ba4e4e5dec254a092b84c");
                List<JdyPurchaseOrder> jdyPurchaseOrders = PurchaseCommunity.queryBased(parameterDelete, headers, vvPuPurchaseRequisition.getDocumentcode(), "documentcode");
                if (jdyPurchaseOrders.size() > 0) {
                    vvPuPurchaseRequisitionMapper.insertPurchaseRequisition(vvPuPurchaseRequisition);
                    for (VvPuPurchaseRequisitionB vvPuPurchaseRequisitionB : vvPuPurchaseRequisition.getVvPuPurchaseRequisitionBS()) {
                        vvPuPurchaseRequisitionBMapper.insertPurchaseRequisitionB(vvPuPurchaseRequisitionB);
                    }

                    Map<String, Object> parameterInster = new HashMap<>();
                    parameterInster.put("app_id", "64f00ef131adb70007ad6e6d");
                    parameterInster.put("entry_id", "650ba4e4e5dec254a092b84c");
                    parameterInster.put("data_id", jdyPurchaseOrders.get(0).get_id());
                    parameterInster.put("data", ConnmeUtils.convertEntityToMap(vvPuPurchaseRequisition));
                    String jsonSale = JSONUtil.toJsonStr(parameterInster);
                    ApiClient.sendPostRequest(KeyConstant.Jian_URL + "/v5/app/entry/data/update", headers, jsonSale);
                    i++;
                }
            }

        }
    }

}
